<?php

namespace app\api\controller;

use app\api\model\TplSlogan;
use think\Db;

class Tpl extends Common
{
    public function slogan()
    {
        $data = TplSlogan::where([
            'types' => $this->params['types'],
            'is_hidden' => 0
        ])->field('id, channel, content')->orderRaw('rand()')->find();

        return $this->pass($data);
    }



    // 创建用户
    public function createuser($userinfo = [])
    {
        if (isset($userinfo['n'])) {
            $uid = Db::name('user')->where(['name'=>$userinfo['n']])->value('id');
            if ($uid) return $uid;
        }
        $createuserphone = cache('createuserphone');
        $createuserphone = $createuserphone ? $createuserphone : 14400000000;
        $hasphone = 1;
        while ($hasphone) {
            ++$createuserphone;
            $hasphone = Db::name('user')->where(['phone'=>$createuserphone])->value('id');
        }

        //生成唯一邀请码
        for ($k = 1; $k < 2; $k++) {
            $code = code();

            $test = Db::table('user')->where(['yqm' => $code])->select();
            if ($test) {
                $k--;
                break;
            }
        }
        switch (rand(0,2)) {
            case 0:
                $name = _nickname(rand(1,2));
                break;
            case 1:
                $name = _nickname2();
                break;
            
            default:
                $name = suiji();
                break;
        }
        $post['yqm'] = $code;
        $post['name'] = $userinfo['n'] ?? $name;
        $post['role'] = $userinfo['r'] ?? 1;
        $post['phone'] = $createuserphone;
        $post['password'] = md5($code);
        $sex=[
            0=>'女',
            1=>'男',
        ];
        $post['sex'] = $userinfo['sex'] ?? $sex[random_int(0,1)];
        $post['head_100'] = $post['head_300'] = $post['head_portrait'] = $userinfo['a'] ?? '/public/style/api/img/female/'.random_int(1,18).'.jpg';
        $post['self_slogan'] = $userinfo['s'] ?? '';

        $db = Db::name('user')->insertGetId($post);
        cache('createuserphone', $createuserphone, 7 * 86400);
        return $db;
    }

    public function to_like()
    {
        $data = Db::name('user_like_list')->where(['sex'=>$this->member['sex']??'男'])->column('title');
        return $this->pass($data);
    }
}
